using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class BRTExport : MonoBehaviour 
{
    public string file = "";

    private StreamWriter sw;
    private static int framesIdx = 0;
    private static int bonesCount = 0;

    // Start is called before the first frame update
    void Start()
    {
        sw = new StreamWriter(@"Assets\Resources\Brt\" + file + ".brt");

        foreach (Transform child in GetComponentsInChildren<Transform>())
        {
            string[] tn = child.name.Split(':');
            if (tn.Length > 1)
                bonesCount++;
        }

        sw.WriteLine("bonesCount:" + bonesCount);

        // Tpose
        sw.WriteLine("Tpose:");
        foreach (Transform child in GetComponentsInChildren<Transform>())
        {
            Vector3 rot = child.localRotation.ToEuler();
            string name = child.name; 
            string[] tn = name.Split(':');
            if (tn.Length <= 1)
                continue;
            name = tn[1];
            sw.WriteLine(name + " " + rot.x + " " + rot.y + " " + rot.z);
        }
    }

    // Update is called once per frame
    void Update()
    {
        sw.WriteLine("frame:" + framesIdx++);
        foreach (Transform child in GetComponentsInChildren<Transform>())
        {
            Vector3 localRotation = child.localRotation.ToEuler();
            //Vector3 rotation = child.rotation.ToEuler();

            string name = child.name;
            string[] tn = name.Split(':');
            if (tn.Length > 1)
                name = tn[1];
            else
                continue;
            sw.WriteLine(name + " " + localRotation.x + " " + localRotation.y + " " + localRotation.z);
            //sw.WriteLine(name + " " + rotation.x + " " + rotation.y + " " + rotation.z);
        }
    }
}